﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using CM.BL.BE;
using System.Collections.Generic;
using Newtonsoft.Json;
using CM.BL.BC;

namespace WebAppHTML5
{
    public partial class detail : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string pRetval = "";
            if (!Request.HttpMethod.Equals("POST"))
            {
                                
            }
            else
            {
                pRetval = "{\"success\":{0}, \"errors\":{\"reason\": \"{1}\"},  ";
                pRetval += "\"detail\": {2} ";
                pRetval += "}";
                string _retval = "";
                string result = detailx(out _retval);
                if (result != "")
                {
                    pRetval = pRetval.Replace("{0}", "false");
                    pRetval = pRetval.Replace("{1}", _retval);
                    pRetval = pRetval.Replace("{2}", "null");
                }
                else
                {
                    pRetval = pRetval.Replace("{0}", "true");
                    pRetval = pRetval.Replace("{1}", _retval);
                    pRetval = pRetval.Replace("{2}", result);
                }
            }

            Response.ContentType = "application/json; charset=UTF-8";
            Response.Write(pRetval);
        }

        private string detailx(out string _retval)
        {
            string retval = string.Empty; string po = string.Empty, m = string.Empty;
            string obj = "";
            po = Request.Params["po"].ToString();
            List<PurchaseOrderBE> lstPO = new List<PurchaseOrderBE>(); 
            PurchaseOrderBE pObj = null;
            retval = PurchaseOrderBC.getInstance(Session.SessionID).getItemsForRelease(out lstPO);
            if (lstPO != null && lstPO.Count > 0)
            {
                if (po != null || po.Trim() != "")
                {
                    pObj = lstPO.Find(X => X.PO == po);    
                }
                
                if (pObj != null)
                {
                    obj = JsonConvert.SerializeObject(pObj);                    
                }
                else
                {
                    obj = "";
                }
            }
            _retval = retval;
            return obj;
        }
    }
}
